package com.zayats.controller;

import java.security.Principal;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zayats.dal.ApiFamilyRepository;
import com.zayats.exceptions.DataAccessDbException;
import com.zayats.exceptions.FamilyNotExistsException;
import com.zayats.model.Family;
import com.zayats.model.User;

@Controller
@RequestMapping(value = "home/families")
public class FamilliesController extends AutorizedController {

	String errors;

	@Autowired
	ApiFamilyRepository familyRepository;

	private static final Logger logger = Logger
			.getLogger(FamilliesController.class);

	@RequestMapping(value = "")
	public String families(Model model, Principal principal) {
		logger.info("Getting families for user");

		model.addAttribute("navigation", naviMap);
		model.addAttribute("title", "Families");
		String username = principal.getName(); // get logged in username
		model.addAttribute("username", username);

		List<Family> list = familyRepository.getFamiliesForUser(username);

		if (list.size() == 0)
			list = null;

		model.addAttribute("familyList", list);
		model.addAttribute("errors", errors);
		return "families";
	}

	@RequestMapping(value = "/{familyId}")
	public String familyManager(@PathVariable String familyId, Model model,
			Principal principal) {
		logger.info("Getting family participans for family manager");

		model.addAttribute("navigation", naviMap);
		model.addAttribute("title", "Family Manager");
		String username = principal.getName(); // get logged in username
		model.addAttribute("username", username);
		model.addAttribute("familyId", familyId);

		List<User> list = null;
		try {
			list = familyRepository.getParticipans(Integer.parseInt(familyId),
					username);
		} catch (FamilyNotExistsException e) {
			model.addAttribute("errors", e.getMessage());
			logger.info("No such family");
			return "family_manager";
		} catch (EmptyResultDataAccessException e) {
			model.addAttribute("errors",
					"There are no users in family. You can invite someone.");
		}

		if (list.size() == 0)
			list = null;

		model.addAttribute("users", list);
		return "family_manager";
	}

	@RequestMapping(value = "/add")
	public String addFamily(@RequestParam("name") String name, Model model,
			Principal principal) {
		logger.info("Creating new family");

		model.addAttribute("navigation", naviMap);
		model.addAttribute("title", "Families");
		String username = principal.getName(); // get logged in username
		model.addAttribute("username", username);

		try {
			if (familyRepository.createFamily(name, username) == false) {
				errors = "You have already had family with such name. Type another name for family.";
			}
		} catch (DataAccessDbException e) {
			errors = e.getMessage();
		}

		return "redirect:/home/families";
	}

	@RequestMapping(value = "/delete/{familyId}")
	public String deleteFamily(@PathVariable String familyId, Model model,
			Principal principal) {
		logger.info("Delete family");

		model.addAttribute("navigation", naviMap);
		model.addAttribute("title", "Families");
		String username = principal.getName(); // get logged in username
		model.addAttribute("username", username);

		try {
			familyRepository.deleteFamily(Integer.parseInt(familyId));
		} catch (DataAccessDbException e) {
			model.addAttribute("error", e.getMessage());
		}
		return "redirect:/home/families";
	}
}
